<?php

namespace app\admin\controller;

use app\admin\model\Admin as AdminModel;
use app\admin\controller\Common;
use think\Request;

class Admin extends Common
{

	//删除
    public function del($id){
		$admin = new AdminModel();
		$delnum = $admin->deladmin($id);
		if ($delnum == '1') {
			$this->success('删除管理员成功',url('lst'));
		}else{
			$this->error('删除管理员失败');
		}
	}

    public function edit($id){
		$admins = db('admin')->find($id);
		if (request()->isPost()) {
			$data = input('post.');
			$validate = \think\Loader::validate('Admin');
			if (!$validate->scene('edit')->check($data)) {
				$this->error($validate->getError());
			}
			$admin = new AdminModel();
			$res = $admin->saveadmin($data,$admins);
			if ($res !== false) {
				$this->success('修改成功',url('lst'));
			}else{
				$this->error('修改失败');
			}
			return;
		}
		$this->assign('admin',$admins);
		if (!$admins) {
			$this->error('该管理员不存在');
		}
        $this->assign('admin',$admins);
		return view();
	}

    public function lst(){
		$admin = new AdminModel();
		$adminres = $admin->getadmin();
		$this->assign('adminres',$adminres);
		return view();
    }
    
    public function add(){
        if (request()->isPost()) {
			$data = input('post.');
			$validate = \think\Loader::validate('Admin');
			if (!$validate->scene('add')->check($data)) {
				$this->error($validate->getError());
			}
			$admin = new AdminModel();
			$res = $admin->addadmin(input('post.'));
			if ($res) {
				$this->success('添加管理员成功',url('lst'));
			}else {
				$this->error('添加管理员失败');
			}
			return;
		}
        return view();
	}
	
	public function logout(){
		//清除session
		session(null);
		$this->success('退出系统成功','login/index');
	}
}
